class PagesController < RootController
  include ApplicationHelper
  
  before_filter :create_brain_buster, :only => [:list]
  before_filter :validate_brain_buster, :only => [:note]

  # list public pages, 
  # user logged in, list public&private user pages  
  def list  
    @pagination_opts = { :prev_label   => '&laquo; ',
                         :next_label   => ' &raquo;',
                         :inner_window => 3, 
                         :outer_window => 1 }
    
    #loged, first personal page
    #loged, params[:friend]
    #not loged, first page
    #not loged, params[:friend]

    if params[:friend].blank?
       if logged_in?
          @cond = "friend_id = #{current_friend.id}"      
       else
          @cond = "public = 1"
       end
    else
    @friend = Friend.find_by_login(params[:friend])
       if @friend          
         if logged_in?
           @cond = "friend_id = #{@friend.id}"
           @friend = nil if params[:friend]==current_friend.login
         else
           @cond = "public = 1 and friend_id=#{@friend.id}"  
         end
       else
           @cond = "public = 1"
       end     
    end

    @pages = Page.list params[:page]||1, @cond
    #redirect_to :controller => 'mind/pages', :action => 'new' if (@pages.empty? and logged_in?)
    @page = @pages.first

  end


  def about
    @pagecount = Page.count :conditions => "public = 1"
  end

  def allinone
    @pages = Page.find(:all, :conditions => "public = 1")
  end

  #notes alias comments
  def note
   @current_page = params[:current_page] || "1"    
   @page = Page.find(params[:id])
   if request.post? and @page
     @note = @page.notes.build
     @note.name = params[:note][:name]
     @note.email = params[:note][:email]            
     @note.website = params[:note][:website] || ""     
     @note.friend = current_friend if logged_in?
     flash[:note] = _ "Email required!" if params[:note][:email].blank?
     if not params[:note][:content].blank?
       @note.content = params[:note][:content]
       @note.remote_ip = request.remote_ip.to_s
       if not @note.save   
         flash[:note] = _("Error.") + "<br/>" + @note.errors.full_messages.join('<br/>')
       else
         flash[:noted] = ""
       end
     else 
      flash[:note] = _ "Content required!"
     end    
   end  
   if params[:friend].blank?
     redirect_to p_url(params[:current_page])
   else
     redirect_to fp_url(params[:friend], params[:current_page])
   end

  end

  def denote
    if request.post? and (@note = Note.find(params[:id]))
      @note.destroy 
       redirect_to pags_url
    else
      redirect_to :action => 'edit', :id => params[:id]
    end
  end

  private 
  
  def render_or_redirect_for_captcha_failure
    @@brain_buster_failure_message = "No not good"
    redirect_to :action => "list"
  end
  
end
